package cn.px.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import cn.px.common.core.utils.StringUtils;
import cn.px.common.security.annotation.InnerAuth;
import cn.px.system.api.domain.DeviceInfoReqVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.px.common.log.annotation.Log;
import cn.px.common.log.enums.BusinessType;
import cn.px.common.security.annotation.RequiresPermissions;
import cn.px.base.domain.DeviceInfo;
import cn.px.base.service.IDeviceInfoService;
import cn.px.common.core.web.controller.BaseController;
import cn.px.common.core.web.domain.AjaxResult;
import cn.px.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.px.common.core.web.page.TableDataInfo;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 设备信息Controller
 *
 * @author 品讯科技
 * @date 2024-08
 */
@Api(tags = "设备信息")
@RestController
@RequestMapping("/device")
public class DeviceInfoController extends BaseController {
    @Autowired
    private IDeviceInfoService deviceInfoService;

    /**
     * 查询设备信息列表
     */
    @ApiOperation(value = "查询设备信息列表", response = DeviceInfo.class)
    @RequiresPermissions("base:info:list")
    @GetMapping("/list")
    public TableDataInfo list(DeviceInfo deviceInfo) {
        startPage();
        List<DeviceInfo> list = deviceInfoService.selectDeviceInfoList(deviceInfo);
        return getDataTable(list);
    }

    /**
     * 通过通道Id查询设备信息列表
     */
    @ApiIgnore
    @InnerAuth
    @ApiOperation(value = "通过通道Id查询设备信息列表", response = DeviceInfo.class)
    @RequiresPermissions("base:info:list")
    @GetMapping("/getDeviceListByGateId")
    public AjaxResult getDeviceListByGateId(String gateId) {
        List<DeviceInfo> list = deviceInfoService.getDeviceListByGateId(gateId);
        return AjaxResult.success(list);
    }

    /**
     * 导出设备信息列表
     */
    @ApiOperation(value = "导出设备信息列表")
    @RequiresPermissions("base:info:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceInfo deviceInfo) {
        List<DeviceInfo> list = deviceInfoService.selectDeviceInfoList(deviceInfo);
        ExcelUtil<DeviceInfo> util = new ExcelUtil<DeviceInfo>(DeviceInfo.class);
        util.exportExcel(response, list, "设备信息数据");
    }

    /**
     * 获取设备信息详细信息
     */
    @ApiOperation(value = "获取设备信息详细信息", response = DeviceInfo.class)
    @RequiresPermissions("base:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(deviceInfoService.selectDeviceInfoById(id));
    }

    /**
     * 新增设备信息
     */
    @ApiOperation(value = "新增设备信息")
//    @RequiresPermissions("base:info:add")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceInfo deviceInfo) {
        return toAjax(deviceInfoService.insertDeviceInfo(deviceInfo));
    }

    /**
     * 修改设备信息
     */
    @InnerAuth
    @ApiOperation(value = "修改设备信息")
    @RequiresPermissions("base:info:edit")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceInfo deviceInfo) {
        return toAjax(deviceInfoService.updateDeviceInfo(deviceInfo));
    }

    /**
     * 删除设备信息
     */
    @InnerAuth
    @ApiOperation(value = "删除设备信息")
    @RequiresPermissions("base:info:remove")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(deviceInfoService.deleteDeviceInfoByIds(ids));
    }


    @InnerAuth
    @ApiOperation(value = "删除设备信息")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @PostMapping("/deleteDeviceInfo")
    public AjaxResult deleteDeviceInfo(String ids) {
        String[] arr = ids.split(",");
        return toAjax(deviceInfoService.deleteDeviceInfoByIds(arr));
    }

    /**
     * 通过设备编号获取设备信息
     */
    @ApiOperation(value = "通过设备编号获取设备信息", response = DeviceInfo.class)
//    @RequiresPermissions("base:info:query")
    @PostMapping(value = "/getDeviceListByDeviceNo")
    public AjaxResult getDeviceListByDeviceNo(@Param("deviceNo") String deviceNo) {
        return success(deviceInfoService.getDeviceListByDeviceNo(deviceNo));
    }

    /**
     * 添加设备信息
     */
    @ApiIgnore
    @InnerAuth
    @ApiOperation(value = "添加设备信息")
    @Log(title = "添加设备信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/saveDeviceInfo")
    public AjaxResult saveDeviceInfo(@RequestBody DeviceInfo deviceInfo) {
        return toAjax(deviceInfoService.insertDeviceInfo(deviceInfo));
    }

    @ApiIgnore
    @InnerAuth
    @ApiOperation(value = "批量添加设备信息")
    @Log(title = "批量添加设备信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/saveDeviceInfoList")
    public AjaxResult saveDeviceInfoList(@RequestBody String deviceStr) {
        logger.info("deviceStr=" + deviceStr);
        if (StringUtils.isBlank(deviceStr)) {
            return toAjax(0);
        }
        List<DeviceInfo> list = JSONObject.parseObject(deviceStr, new TypeReference<List<DeviceInfo>>(){});
        return AjaxResult.success(deviceInfoService.insertDeviceInfoList(list));
    }

    @ApiIgnore
    @InnerAuth
    @ApiOperation(value = "绑定设备信息")
    @Log(title = "绑定设备信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/bindDevice")
    public AjaxResult bindDevice(@RequestParam("parkingId") String parkingId, @RequestParam("gateStr") String gateStr, @RequestParam("deviceId") String deviceId) {
        if (StringUtils.isBlank(parkingId) || StringUtils.isBlank(gateStr)
                || StringUtils.isBlank(deviceId)) {
            return toAjax(0);
        }
        int n = deviceInfoService.bindDevice(parkingId, gateStr, deviceId);
        logger.info("n=" + n);
        return AjaxResult.success(n);
    }

    /**
     * 查询设备信息列表
     */
    @ApiIgnore
    @InnerAuth
    @ApiOperation(value = "查询设备信息列表", response = DeviceInfo.class)
    @PostMapping("/getDeviceInfoList")
    public AjaxResult getDeviceInfoList(@RequestBody DeviceInfoReqVO deviceInfoReqVO) {
        List<DeviceInfo> list = deviceInfoService.getDeviceInfoList(deviceInfoReqVO);
        return AjaxResult.success(list);
    }



    @ApiIgnore
    @InnerAuth
    @ApiOperation(value = "更新设备状态信息")
    @Log(title = "更新设备状态信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateDeviceInfoList")
    public AjaxResult updateDeviceInfoList(@RequestBody String deviceStr) {
        logger.info("deviceStr=" + deviceStr);
        if (StringUtils.isBlank(deviceStr)) {
            return toAjax(0);
        }
        List<DeviceInfo> list = JSONObject.parseObject(deviceStr, new TypeReference<List<DeviceInfo>>(){});
        list.stream().forEach(item -> {
            deviceInfoService.updateDeviceInfo(item);
        });
        return AjaxResult.success(list.size());
    }


    @ApiIgnore
    @InnerAuth
    @ApiOperation(value = "通过父级编码更新设备信息")
    @Log(title = "通过父级编码更新设备信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateDeviceInfoByParentCode")
    public AjaxResult updateDeviceInfoByParentCode(String indexCode, String ip ) {
        if (StringUtils.isBlank(indexCode) || StringUtils.isBlank(ip)) {
            return toAjax(0);
        }
        int num = deviceInfoService.updateDeviceInfoByParentCode(indexCode, ip);
        return AjaxResult.success(num);
    }




    @ApiIgnore
    @InnerAuth
    @ApiOperation(value = "通过ip查询设备信息")
    @PostMapping(value = "/getDeviceInfoByIp")
    public AjaxResult getDeviceInfoByIp(String ip ) {
        if (StringUtils.isBlank(ip)) {
            return AjaxResult.error("参数不能为空");
        }
        DeviceInfo deviceInfo = deviceInfoService.getDeviceInfoByIp(ip);
        return AjaxResult.success(deviceInfo);
    }


    @ApiIgnore
    @InnerAuth
    @ApiOperation(value = "查询设备信息列表", response = DeviceInfo.class)
    @PostMapping("/selectDeviceList")
    public AjaxResult selectDeviceList() {
        DeviceInfo deviceInfo = new DeviceInfo();
        List<DeviceInfo> list = deviceInfoService.selectDeviceInfoList(deviceInfo);
        return AjaxResult.success(list);
    }


    @InnerAuth
    @ApiOperation(value = "修改设备信息")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PostMapping("/editDeviceInfo")
    public AjaxResult editDeviceInfo(@RequestBody DeviceInfo deviceInfo) {
        deviceInfo.setIsEnable(1);
        int num = deviceInfoService.updateDeviceInfo(deviceInfo);
        return AjaxResult.success(num);
    }

}
